package com.huichaoliu.stylist.activity;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.adapters.FragmentShoppingAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.UtilInfo;
import com.huichaoliu.view.Mpop;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShoppingIncludeFragment2 extends Fragment implements
		OnClickListener {
	// tools
	Context context;

	// Views
	View v;
	LinearLayout r_cost;
	LinearLayout r_season;
	LinearLayout r_type;

	TextView t_cost;
	TextView t_season;
	TextView t_type;

	GridView gridView;
	TextView t_btn;

	// adapter
	FragmentShoppingAdapter adapter;

	// values
	List<UtilInfo> list;

	// pop
	Mpop pop_cost;
	Mpop pop_season;
	Mpop pop_type;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (v == null) {
			v = inflater.inflate(R.layout.shopping_include_fragment2,
					container, false);
		}
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeView(v);
		}
		context = getActivity();
		initView();
		getData();
		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dissmissAll();
			}
		});
		return v;
	}

	private void initView() {
		r_cost = (LinearLayout) v.findViewById(R.id.shopping_filter_cost);
		r_season = (LinearLayout) v.findViewById(R.id.shopping_filter_season);
		r_type = (LinearLayout) v.findViewById(R.id.shopping_filter_type);

		t_cost = (TextView) v.findViewById(R.id.shopping_filter_cost_txt);
		t_season = (TextView) v.findViewById(R.id.shopping_filter_season_txt);
		t_type = (TextView) v.findViewById(R.id.shopping_filter_type_txt);

		gridView = (GridView) v.findViewById(R.id.shopping_grid);
		t_btn = (TextView) v.findViewById(R.id.shopping_click_btn);

		r_cost.setOnClickListener(this);
		r_season.setOnClickListener(this);
		r_type.setOnClickListener(this);
		t_btn.setOnClickListener(this);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				dissmissAll();
				list.get(arg2).setCheck(!list.get(arg2).isCheck());
				adapter.notifyDataSetChanged();
			}
		});
	}

	private void getData() {
		list = new ArrayList<UtilInfo>();
		for (int i = 0; i < 11; i++) {
			list.add(new UtilInfo());
		}

		adapter = new FragmentShoppingAdapter(list, context);
		gridView.setAdapter(adapter);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.shopping_filter_cost:
			if (pop_cost == null) {
				pop_cost = new Mpop(context, r_cost.getWidth(),null);
			}
			if (pop_season != null) {
				pop_season.dismiss();
			}

			if (pop_type != null) {
				pop_type.dismiss();
			}
			if (pop_cost.isShowing()) {
				pop_cost.dismiss();
			} else {
				pop_cost.showPop(Mpop.COST, t_cost);
			}
			break;
		case R.id.shopping_filter_season:
			if (pop_season == null) {
				pop_season = new Mpop(context, r_season.getWidth(),null);
			}
			if (pop_cost != null) {
				pop_cost.dismiss();
			}

			if (pop_type != null) {
				pop_type.dismiss();
			}
			if (pop_season.isShowing()) {
				pop_season.dismiss();
			} else {
				pop_season.showPop(Mpop.SEASON, t_season);
			}

			break;
		case R.id.shopping_filter_type:
			if (pop_type == null) {
				pop_type = new Mpop(context, r_type.getWidth(),null);
			}
			if (pop_cost != null) {
				pop_cost.dismiss();
			}

			if (pop_season != null) {
				pop_season.dismiss();
			}
			if (pop_type.isShowing()) {
				pop_type.dismiss();
			} else {
				pop_type.showPop(Mpop.TYPE, r_type);
			}
			break;
		case R.id.shopping_click_btn:
			((Activity) context).finish();
			break;

		default:
			break;
		}
	}

	public void dissmissAll() {
		if (pop_cost != null) {
			pop_cost.dismiss();
		}

		if (pop_season != null) {
			pop_season.dismiss();
		}

		if (pop_type != null) {
			pop_type.dismiss();
		}
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (hidden) {
			dissmissAll();
		}
	}

	public void setText(String str) {
		if (t_btn != null) {
			t_btn.setText(str);
		}
	}

}
